<?php

namespace App\Http\Controllers;



use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ApiController extends Controller
{
    protected $token = null;
    protected $request;
    protected $user_id;
    protected static $returnCode, $returnData, $returnMsg, $returnExt;


    public function __construct(Request $request)
    {
        $this->request = $request;
        //$this->token = $request->header("authorization", null);
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Headers:Content-Type,Accept,Authorization');
        header('Access-Control-Allow-Methods:GET,POST,PUT,OPTIONS');
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
            return self::returnAjax('Origin Allowed !', 500);
        }
    }

    protected static function ajaxReturn($code = 200, $data = null, $msg = null, $ext = null)
    {
        return response()->json([
            'code' => $code ?: self::$returnCode,
            'data' => $data ?: self::$returnMsg,
            'msg' => $msg ?: self::$returnMsg,
            'ext' => $ext ?: self::$returnExt
        ]);
    }


    protected function returnAjax($data = [], $code = 200, $msg = 'success')
    {
        return response()->json(['data' => $data, 'code' => $code, 'msg' => $msg], 200);
    }

    /**
     * @SWG\Post(path="/api/upload",
     *   tags={"Others"},
     *   summary="图片上传",
     *   description="图片上传",
     *   operationId="uploadFile",
     *   produces={"application/json"},
     *@SWG\Parameter(
     *     in="formData",
     *     name="user_id",
     *     type="integer",
     *     description="用户ID",
     *     required=true
     *   ),
     *@SWG\Parameter(
     *     in="formData",
     *     name="token",
     *     type="string",
     *     description="token",
     *     required=true
     *   ),
     *@SWG\Parameter(
     *     in="formData",
     *     name="file",
     *     type="string",
     *     description="file",
     *     required=true
     *   ),
     *   @SWG\Response(response="200", description="图片上传成功"),
     *   @SWG\Response(response="201", description="图片上传失败")
     *
     * )
     */
    public function uploadFile()
    {
        $file = $this->request->file('head_image');
        $fileName = uniqid(str_random(8)) . '.' . $file->getClientOriginalExtension();
        $oss = new AliyunOssService();
        $head_image = $oss->upload($fileName, $file);
        return $head_image ? self::ajaxReturn(200, $head_image, '图片上传成功') : self::ajaxReturn(201, $head_image, '图片上传失败');
    }

    /**
     * 业务层计数器
     * @param $key 计数器key
     * @param null $step 级数步子
     * @param int $expiration 有效期
     * @return Int count
     */
    protected function counter($key,$step=null,$expiration=1440){

        $count = Cache::get($key,0);
        /*直接获取值*/
        if( $step === null ){
            return $count;
        }

        $count = $count + $step;

        Cache::put($key,$count,$expiration);

        return $count;
    }
}
